<template>
  <div class="loginbgc">
    <el-card class="box-card">
      <div class="logo">
        <img src="../assets/logo.png" />
      </div>
      <el-form
        :model="LoginForm"
        :rules="rules"
        ref="LoginForm"
        label-width="0"
        class="login-form"
      >
        <el-form-item prop="username">
          <el-input
            v-model="LoginForm.username"
            prefix-icon="el-icon-user"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            type="password"
            v-model="LoginForm.password"
            prefix-icon="el-icon-key"
            show-password
            autocomplete="off"
            @keyup.enter.native="KeyDown('LoginForm')"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('LoginForm')"
            >登录</el-button
          >
          <el-button @click="resetForm('LoginForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      LoginForm: {
        username: 'admin',
        password: 'admin',
      },
      rules: {
        username: [
          { required: true, message: '请输入登录名称', trigger: 'blur' },
          {
            min: 3,
            max: 10,
            message: '长度在 3 到 10 个字符',
            trigger: 'blur',
          },
        ],
        password: [
          { required: true, message: '请输入登录密码', trigger: 'blur' },
          {
            min: 3,
            max: 10,
            message: '长度在 6 到 15 个字符',
            trigger: 'blur',
          },
        ],
      },
    }
  },
  methods: {
    KeyDown(e) {
      if (event.keyCode === 13) {
        this.submitForm(e)
      }
    },
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (!valid) return
        const { data: res } = await this.$http.post('login.php', {
          user: this.LoginForm.username,
          pwd: this.LoginForm.password,
        })
        console.log('res', res)
        if (res.meta.status !== 200) return this.$message.error('登录失败！')
        this.$message.success('登录成功')
        window.sessionStorage.setItem('token', res.token)
        this.$router.push('home')
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
  },
}
</script>

<style lang="less" scoped>
.loginbgc {
  height: 100%;
  background: url(../assets/img/login_bgc.jpg);
  .el-card {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 90%;
    max-width: 520px;
    transform: translate(-50%, -50%);
    text-align: center;
    border-radius: 10px;
    .logo {
      position: relative;
      display: inline-block;
      background-color: rgba(243, 152, 0, 0.2);
      border-radius: 50%;
      margin: 15px;
      img {
        width: 100px;
        height: 100px;
        object-fit: contain;
      }
      &::after {
        position: absolute;
        z-index: -1;
        content: '';
        top: -3px;
        bottom: -3px;
        left: -3px;
        right: -3px;
        background-color: rgba(243, 152, 0, 0.4);
        border-radius: 50%;
        animation: hx_scale 2s linear infinite;
      }
      &::before {
        position: absolute;
        z-index: -1;
        content: '';
        top: -18px;
        bottom: -18px;
        left: -18px;
        right: -18px;
        background-color: rgba(243, 152, 0, 0.2);
        border-radius: 50%;
        animation: hx_scale 2s linear infinite;
      }
    }
  }
}

@keyframes hx_scale {
  0% {
    transform: scale(0);
    opacity: 0;
  }
  50% {
    transform: scale(0.5);
    opacity: 1;
  }
  100% {
    transform: scale(1.1);
    opacity: 0;
  }
}
</style>
